//
//  BKCommentView.m
//  XPhoto
//
//  Created by baboy on 2019/7/3.
//  Copyright © 2019 baboy. All rights reserved.
//

#import "BKCommentView.h"

@interface BKCommentView()
@property(nonatomic, assign) BOOL fullscreen;
@property(nonatomic, assign) CGRect textViewOriginBounds;
@property(nonatomic, assign) CGRect keyboardBounds;
@property(nonatomic, assign) CGFloat animationDuration;
@property(nonatomic, assign) NSInteger animationCurve;
@property(nonatomic, assign) CGFloat maxTextViewHeight;
@end

@implementation BKCommentView
- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addKeyboardOBserver];
    }
    return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self addKeyboardOBserver];
    }
    return self;
}
- (void)awakeFromNib{
    [super awakeFromNib];
    self.textViewOriginBounds = self.textView.bounds;
    NSString *testStr = @"测试p\n测试p\n测试p\n测试p\n测试p";
    CGSize size = [testStr sizeWithAttributes:@{NSFontAttributeName:self.textView.font}];
    self.maxTextViewHeight = size.height;
}
- (void)layoutSubviews{
    [super layoutSubviews];
//    [self layoutSubviewsWithKeyboardPopup:NO];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)addKeyboardOBserver{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
#pragma keyboard listener
- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect keyboardBounds;
    [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [notification.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];
    self.animationCurve = [curve intValue];
    self.animationDuration = [duration doubleValue];
    
    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self convertRect:keyboardBounds toView:nil];
    self.keyboardBounds = keyboardBounds;
    [self layoutSubviewsWithKeyboardPopup:YES];
}
- (void)keyboardWillHide:(NSNotification *)notification{
    [self layoutSubviewsWithKeyboardPopup:NO];
}
- (void)layoutSubviewsWithKeyboardPopup:(BOOL)keyboardPopup{
    CGSize textViewContentSize = self.textView.contentSize;
    textViewContentSize.height = MAX(textViewContentSize.height, self.textViewOriginBounds.size.height);
    textViewContentSize.height = MIN(self.maxTextViewHeight, textViewContentSize.height);
    
    
    CGRect bottomFrame = self.bottomPannel.frame;
    bottomFrame.size.height = 2*self.textViewBackgoundView.frame.origin.y + 2*self.textView.frame.origin.y + textViewContentSize.height;
    
    CGFloat contentTop = self.superview.bounds.size.height - bottomFrame.size.height - (keyboardPopup?self.keyboardBounds.size.height:self.superview.safeAreaInsets.bottom);
    if (self.fullscreen && !keyboardPopup) {
        contentTop = self.superview.bounds.size.height;
    }
    
    
    CGRect frame = CGRectMake(0, contentTop, bottomFrame.size.width, self.superview.bounds.size.height - contentTop);
    
    if (self.fullscreen) {
        bottomFrame.origin.y = contentTop;
    }
    // animations settings
    [UIView animateWithDuration:self.animationDuration
                          delay:0
                        options:self.animationCurve
                     animations:^{
                         if (!self.fullscreen) {
                             self.frame = frame;
                         }
                         self.bottomPannel.frame = bottomFrame;
                         UIColor *backgroundColor = keyboardPopup ? [UIColor colorWithWhite:0 alpha:0.3] : [UIColor clearColor];
                         self.backgroundView.backgroundColor = backgroundColor;
                     }
                     completion:^(BOOL finished) {
                         if (self.fullscreen && !keyboardPopup) {
                             [self removeFromSuperview];
                         }
                     }];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    DLOG(@"text content size:%@", NSStringFromCGSize(textView.contentSize));
    [self layoutSubviewsWithKeyboardPopup:YES];
    
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textViewShouldBeginEditing:)]) {
        return [self.delegate textViewShouldBeginEditing:self];
    }
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textViewShouldEndEditing:)]) {
        return [self.delegate textViewShouldEndEditing:self];
    }
    return YES;
}
#pragma mark - action
- (IBAction)clickSendButton:(id)button{
    if (self.delegate && [self.delegate respondsToSelector:@selector(commentView:didClickedSendButton:)]) {
        [self.delegate commentView:self didClickedSendButton:button];
    }
    [self.textView resignFirstResponder];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


+ (id)view{
    UIView *view = (BKCommentView*)loadViewFromNib([self class], nil);
    return view;
}
- (void) showWithText:(NSString * _Nullable)text{
    UIView *container = [APP keyWindow];
    self.frame = container.bounds;
    //    [self layout];
    //    [self updateCountLabel];
    DLOG(@"show");
    self.frame = container.bounds;
    self.textView.text = text;
    CGRect bottomFrame = self.bottomPannel.frame;
    bottomFrame.origin.y = self.frame.size.height;
    self.bottomPannel.frame = bottomFrame;
    self.fullscreen = YES;
    [container addSubview:self];
    [self.textView becomeFirstResponder];
}
- (void)show{
    [self showWithText:nil];
}
- (void)hide{
    BOOL hide = [self.textView resignFirstResponder];
    if (!hide && self.bottomPannel.frame.origin.y >= self.bounds.size.height) {
        [self removeFromSuperview];
    }
}
- (IBAction)hide:(id)sender{
    [self hide];
}
@end
